/// <reference types="vite/client" />
import axios from "axios"; 
import type{ AxiosInstance,AxiosRequestConfig } from "axios";
import { resolve } from "path";
import { LoadingService } from "../src/utils/loading";
export const instance: AxiosInstance = axios.create({
  baseURL: import.meta.env.VITE_API_BASEURL || "http://localhost:3000/api",
  timeout: 1000 * 60, // 1 minute
  headers: {
    "Content-Type": "application/json"
  }

});
export default async function $http(url: string, options?: { showLoading?: boolean } & AxiosRequestConfig) {
  const { showLoading = true, ...axiosOptions } = options || {};

  if (showLoading) {
    LoadingService.show();
  }

  return new Promise((resolve, reject) => {
    instance.request({
      url,
      ...axiosOptions
    })
    .then(response => {
      console.log('Response:', response.data);
      resolve(response.data);
    })
    .catch(error => {
      console.error('Request error:', error);
      reject(error);
      // 可以在这里添加统一的错误提示逻辑
    })
    .finally(() => {
      if (showLoading) {
        LoadingService.hide();
      }
    });
  });
}